package com.note.providers;

import java.io.File;
import java.util.HashMap;

import com.note.provider.Ink;
import com.note.provider.Ink.Inks;
import com.note.providers.InkDatabaseHelper.Tables;
import com.note.providers.InkDatabaseHelper.Views;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

public class InkProvider extends ContentProvider {

	SQLiteOpenHelperEx mOpenHelper;
	
	private static final int INKS = 2000;
	private static final int INKS_ID = 2001;
	private static final int BATCH = 3000;
	
	private static final UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
	static {
        final UriMatcher matcher = sUriMatcher;
        matcher.addURI(Ink.AUTHORITY, "inks", INKS);
        matcher.addURI(Ink.AUTHORITY, "inks/#", INKS_ID);
        matcher.addURI(Ink.AUTHORITY, "inks/batch/#", BATCH);
	};
	
	private static final HashMap<String, String> sInkProjectionMap;
	static {
		sInkProjectionMap = new HashMap<String, String>();
		sInkProjectionMap.put(Inks._ID, Inks._ID);
		sInkProjectionMap.put(Inks._COUNT, "COUNT(*)");
		sInkProjectionMap.put(Inks.BATCH, Inks.BATCH);
		sInkProjectionMap.put(Inks.PAGE_NUMBER, Inks.PAGE_NUMBER);
		sInkProjectionMap.put(Inks.CONTENT, Inks.CONTENT);
		sInkProjectionMap.put(Inks.IMAGE_BUFFER, Inks.IMAGE_BUFFER);
		sInkProjectionMap.put(Inks.TEXT_BUFFER, Inks.TEXT_BUFFER);
		sInkProjectionMap.put(Inks.BATCH_COUNT, Inks.BATCH_COUNT);
	}
	
	@Override
	public boolean onCreate() {
		checkDirectory(InkDatabaseHelper.DATABASE_PATH);
		mOpenHelper = new InkDatabaseHelper(getContext());
	   // mOpenHelper.getWritableDatabase();
		return true;
	}
	
	@Override
	public String getType(Uri uri) {
		 switch (sUriMatcher.match(uri)) {
	         case INKS:
	             return Inks.CONTENT_TYPE;
	         case INKS_ID:
	        	 return Inks.CONTENT_ITEM_TYPE;
	         case BATCH:
	        	 return Inks.CONTENT_TYPE;
	         default:
	             throw new IllegalArgumentException("Unknown URI" + uri);
		 }
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
            String sortOrder) {
		
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        switch (sUriMatcher.match(uri)) {
            case INKS:
                qb.setTables(Views.INKS);
                qb.setProjectionMap(sInkProjectionMap);
                break;
            case BATCH:
            	qb.setTables(Views.INKS);
                qb.setProjectionMap(sInkProjectionMap);
                qb.appendWhere(Inks.BATCH + "=" + ContentUris.parseId(uri));
                break;
            default:
            	throw new IllegalArgumentException("Unknown URI" + uri);
        }
            
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		return qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		long rowId;
		
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        switch (sUriMatcher.match(uri)) {
            case INKS:
                rowId = db.insert(Tables.INKS, null, values);
                break;
            default:
            	throw new IllegalArgumentException("Unknown URI" + uri);
        }
        
        if (rowId > 0) {
            Uri newUri = ContentUris.withAppendedId(uri, rowId);
            return newUri;
        } else
            return null;
	}

	@Override
	public int update(Uri uri, ContentValues values, String whereClause, String[] whereArgs) {
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		
		switch (sUriMatcher.match(uri)) {
	        case INKS:
	            return db.update(Tables.INKS, values, whereClause, whereArgs);
	        case INKS_ID:
	            return db.update(Tables.INKS, values, Inks._ID + "=" + ContentUris.parseId(uri), null);
	        default:
	            throw new IllegalArgumentException("Unknown URL " + uri);
	    }
	}
	
	@Override
	public int delete(Uri uri, String whereClause, String[] whereArgs) {
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		
		switch (sUriMatcher.match(uri)) {
	        case INKS:
	            return db.delete(Tables.INKS, whereClause, whereArgs);
	        case BATCH:
	        	return db.delete(Tables.INKS, Inks.BATCH + "=" + ContentUris.parseId(uri), null);
	        default:
	            throw new IllegalArgumentException("Unknown URL " + uri);
	    }
	}
	
	private void checkDirectory(String name) {
		File path = new File(name);
		
		if (!path.exists())
			path.mkdirs();
	}
}
